<template>
  <div class="login">
    <van-nav-bar
      :title="title"
      left-arrow
      @click-left="$router.back()"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <van-cell
      v-for="item in list"
      :key="item.art_id.toString()"
      :title="item.title"
    />
</van-list>
  </div>
</template>
<script>
import { getSearchResults } from '@/api/search'
export default {
  name: 'searchResult',
  data () {
    return {
      list: [],
      finished: false,
      loading: false,
      page: 1
    }
  },
  computed: {
    title () {
      return `${this.$route.params.q}的搜索结果`
    }
  },
  created () {
  },
  methods: {
    async onLoad () {
      const res = await getSearchResults({
        page: this.page,
        per_page: 20,
        q: this.$route.params.q
      })
      const results = res.data.data.results
      this.list.push(...results)
      this.loading = false
      if (results.length) {
        this.page++
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style>

</style>
